<template>
	<transition name="el-zoom-in-top">
		<div class="video-preview-wrapper" v-show="visible">
			<div class="top">
				<div class="video-name">
					{{ activeFileObj.fileName }}.{{ activeFileObj.extendName }}
					<span class="size">{{
              fileFunction.calculateFileSize(activeFileObj.fileSize)
					}}</span>
				</div>
				<n-icon
					@click="handleClosePreview"
          size = '36px'
				>
          <close/>
        </n-icon>
			</div>
			<div class="bottom">
				<VideoPlayer
					class="video-player"
					ref="videoRef"
					:source="activeVideoSource"
					v-if="visible"
				></VideoPlayer>

					<div class="video-list-wrapper">
						<div class="list-title">播放列表</div>
						<ul class="video-list">
							<li
								class="video-item"
								v-for="(item, index) in videoList"
								:key="index"
								:class="[activeIndex === index ? 'active' : '']"
								@click="activeIndex = index"
							>
								<span class="name"
									>{{ item.fileName }}.{{ item.extendName }}</span
								>
								<span class="size">{{
                    fileFunction.calculateFileSize(item.fileSize)
								}}</span>
							</li>
						</ul>
					</div>
			</div>
		</div>
	</transition>
</template>
<script lang="ts" setup>
//@ts-ignore
import fileFunction from '@/libs/globalFunction/file.js'
import {computed, nextTick, ref, watch} from "vue";
import VideoPlayer from './VideoPlayer.vue'
import {Close} from '@vicons/ionicons5'
/**视频组件是否可见*/
const visible = ref(false)
/**当前打开的视频索引*/
const activeIndex = ref(0)
const defaultIndex = ref(0)

const videoRef = ref(null)

const videoList = ref([])


/**当前视频文件信息**/
const activeFileObj = computed(() => {
  console.log(videoList.value[activeIndex.value])
  let res = videoList.value.length ? videoList.value[activeIndex.value] : {}
  return res
})


// 当前播放的视频源信息
const activeVideoSource = computed(() => {
  return {
    type: `video/${videoList.value[activeIndex.value].extendName}`, //  取值文件后缀；这里的种类支持很多种：基本视频格式、直播、流媒体等，具体可以参看 https://videojs.com/getting-started
    src: videoList.value[activeIndex.value].fileUrl // url 地址
  }
})


const previewVideo = (videoList_:[] , defaultIndex_:number) => {
  videoList.value = videoList_
  defaultIndex.value = defaultIndex_
  visible.value = true
}


watch(visible, (newVal) => {
  if (newVal) {
    activeIndex.value = defaultIndex.value
  }
})

const handleClosePreview = () => {
  visible.value = false

}


defineExpose({
  previewVideo
})

</script>


<style lang="stylus" scoped>

.video-preview-wrapper {
  background: rgba(0, 0, 0, 0.75);
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 3;

  .top {
    margin-bottom: 8px;
    background: #000;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 48px;
    line-height: 48px;
    padding: 0 8px 0 16px;

    .video-name {
      flex: 1;
      color: #fff;

      .size {
        margin-left: 16px;
        color: $SecondaryText;
        font-size: 12px;
      }
    }

    .download-link {
      display: block;
      height: 24px;
      line-height: 24px;
    }

    .download-icon, .fold-icon, .close-icon {
      margin-left: 8px;
      width: 32px;
      cursor: pointer;
      font-size: 24px;
      color: #fff;

      &:hover {
        opacity: 0.6;
      }
    }
  }

  .bottom {
    height: calc(100vh - 60px);
    display: flex;
    justify-content: space-between;

    .video-player {
      flex: 1;
      width: 100%;
      height: 100%;
      object-fit: fill;
    }

    .video-list-wrapper {
      margin-left: 8px;
      background: #000;
      width: 280px;
      height: 100%;
      color: #fff;

      .list-title {
        border-bottom: 2px solid $RegularText;
        height: 40px;
        line-height: 40px;
        padding: 0 16px;
      }

      .video-list {
        padding: 8px 0;
        font-size: 12px;
        cursor: pointer;
        height: calc(100% - 42px);
        overflow: auto;
        list-style: none;
        setScrollbar(8px);

        .video-item {
          padding: 8px 16px;
          display: flex;
          justify-content: space-between;

          &:hover {
            color: $Primary;
          }

          .name {
            flex: 1;
            word-break: break-all;
          }

          .size {
            margin-left: 8px;
            width: 56px;
            color: $SecondaryText;
            text-align: right;
          }
        }

        .video-item.active {
          background: #000;
          color: $Primary;
        }
      }
    }
  }
}
</style>
